<template>
  <el-dialog :visible.sync="visible" v-dialogDrag :title="title" class="tl-dialog" width="80%" height="80%" @open="open" @close="close" top="10px" :append-to-body="true" :modal-append-to-body="true">
    <div class="ref-dialog">
      <!-- 查询界面 -->
      <tl-list-search ref="search" :query-param="queryParam" @search="doQuery" :isDisplayDefaultSearch="false">
        <template slot="search">
          <el-form-item label-width="0">
            <el-dropdown @command="clickDropdown">
              <el-button type="primary"> {{ tabList.find(tab => tab.code === activeName).name }}<i class="el-icon-arrow-down el-icon--right"></i> </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click="clickDropdown" v-for="item in tabList" :command="item.code" :key="item.code" :label="item.name" :name="item.code">{{ item.name }}</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-form-item>
          <el-form-item label="企业名称">
            <el-input v-model="queryParam.qry_name" placeholder=""></el-input>
          </el-form-item>
          <el-form-item label="负责部门" prop="qry_deptname">
            <el-input v-model="queryParam.qry_deptname" placeholder=""></el-input>
          </el-form-item>
          <el-form-item label="我的客户" prop="qry_deptname">
            <el-input v-model="queryParam.qry_deptname" placeholder=""></el-input>
          </el-form-item>
          <el-form-item label="企业类型" prop="qry_nature">
            <el-select v-model="queryParam.qry_category" filterable clearable>
              <template v-for="item in natureList">
                <el-option :key="item.code" :label="item.name" :value="item.code" />
              </template>
            </el-select>
          </el-form-item>
        </template>
      </tl-list-search>
      <!-- 列表界面 -->
      <div class="Company">
        <!--表格渲染-->
        <el-table class="tl-table" ref="mainTable" height="30em" v-loading="loading" :data="datas" @selection-change="doSelectChange" @sort-change="doSortChange" @row-dblclick="doRowDbClick" row-key="id" border fit :row-class-name="doChangeRowClassName">
          <el-table-column type="selection" width="55px" fixed="left" v-if="!isSingleSelect"> </el-table-column>
          <el-table-column prop="name" label="企业名称" min-width="150px" sortable="custom" sort-by="name" align="left" header-align="center"></el-table-column>
          <!-- <el-table-column prop="dispnature" label="企业性质" width="200px" sortable="custom" sort-by="nature" align="center" header-align="center"></el-table-column> -->
           <el-table-column prop="dispcategory" label="企业类型" width="200px" sortable="custom" sort-by="category" align="center" header-align="center"></el-table-column>
          <el-table-column prop="deptname" label="负责人所在部门名称" width="150px" sortable="custom" sort-by="deptname" align="left" header-align="center"></el-table-column>
          <el-table-column label="操作" width="80px" align="center" fixed="right" v-if="isSingleSelect === true">
            <template slot-scope="scope">
              <el-button type="text" icon="el-icon-finished" @click="doSelectData(scope.row[fieldDefaultKey])">选择</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 列表分页 -->
        <el-row class="pagebar" ref="pager">
          <el-pagination @size-change="doChangeSize" @current-change="doChangeIndex" :current-page="pageInfo.currentPage" :page-sizes="pageInfo.pageSizes" :total="pageInfo.totalCount" layout="total, sizes, prev, pager, next, jumper" background class="tl-table-pagination"> </el-pagination>
        </el-row>
      </div>
    </div>
    <div slot="footer" class="dialog-footer" v-if="isSingleSelect === false">
      <el-button type="text" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doConfirm">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
/** mixins */
import { mixin_ref_list } from "@tapui/mixins/mixin_ref_list"
/** 子组件 */
import { validateNull } from "@tapui/utils/validate"

/** 工具类 */
import { queryDataCode } from "@tapui/api/dataCode"
import { getAction } from "tap-ui/src/utils/http"
/** VUE配置 */
export default {
  name: "TlCpCustomerCompanyRef",
  components: {},
  mixins: [mixin_ref_list],
  data() {
    return {
      description: "客户选择参照页面",
      // 默认Key字段名
      fieldDefaultKey: "id",
      // 默认显示字段名
      fieldDefaultValue: "name",
      // url配置\
      baseUrl: "/cp/customerCompany",
      url: {
        list: "/cp/customerCompany/refList",
        billInfo: "/cp/customerCompany/BillInfo"
      },
      natureList: [],
      // 排序参数
      sorterInfo: {
        sidx: "createon",
        sord: "desc"
      },
      // 类型列表
      tabList: [{ name: "企业客户", code: "CpCustomerCompany", url: "/cp/customerCompany" }, { name: "企业客户(非电)", code: "CpCustomerCompanyOther", url: "/cp/customerCompanyOther" }],
      // 当前选择的类型
      activeName: "CpCustomerCompany"
    }
  },

  methods: {
    /** 打开对话框 */
    open() {
      this.initRef()
      this.queryCodeModel()
    },
    queryCodeModel() {
      queryDataCode({ itemCodes: "CpNature" }).then(res => {
        this.natureList = res.result.CpNature
      })
    },
    // 点击下拉选单切换类型
    clickDropdown(commandCode) {
      this.activeName = commandCode
      this.baseUrl = this.tabList.find(item => item.code === commandCode).url
      this.url.list = this.baseUrl + "/refList"
      this.url.billInfo = this.baseUrl + "/BillInfo"
      this.doQuery()
    },
    doSelectData(paramKey) {
        this.selectedRows = []
        this.selectedRowKeys = []
        let tempData = this.datas.find((item) => {
            return item[this.fieldKeyComputed] === paramKey
        })
        // TODO 增加tempData空判断
        if (validateNull(tempData)) {
            this.$tap.alert('请选择数据!')
            return
        }
        // 填充数据
        this.selectedRows.push(tempData)
        this.selectedRowKeys.push(tempData[this.fieldKeyComputed])
        let selectedValue = tempData[this.fieldValueComputed]
        this.$emit('confirm', this.selectedRows[0], this.selectedRowKeys[0], selectedValue, this.activeName)
        this.visible = false
    }
  }
}
</script>
<style lang="scss" scoped>
.ref-dialog {
  padding-bottom: 10px;
}
</style>
